<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改轮播图')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-banner-edit" th:object="${jiajiaBanner}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <input id="file" accept="image/jpg,image/jpeg,image/png" name="file"  class="form-control"  type="file">
                    <img style="width: 80px ;height: auto;" th:src="*{image}" id="imgPhoto"  />
                    <input th:field="*{image}" style="display: none" id="image" name="image" class="form-control"   type="text">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">活动介绍：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" class="form-control" name="content" th:field="*{title}">-->
<!--                    <div class="summernote" id="title"></div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group" id="delDiv">
                <label class="col-sm-3 control-label">是否显示</label>
                <div class="col-sm-8">
                    <input type="hidden" id="Del" th:value="*{isDel}">
                    <select id="isDel" name="isDel" class="form-control m-b">
                        <option value="1" th:selected="${jiajiaBanner.isDel == 1}" >否</option>
                        <option value="0" th:selected="${jiajiaBanner.isDel == 0}">是</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片跳转地址：</label>
                <div class="col-sm-8">
                    <input name="url" th:field="*{url}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序，越小越靠前：</label>
                <div class="col-sm-8">
                    <input name="sort" th:field="*{sort}" class="form-control" type="text">
                </div>
            </div>
<!--            <div class="form-group">    -->
<!--                <label class="col-sm-3 control-label">添加时间：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <div class="input-group date">-->
<!--                        <input name="addtime" th:value="${#dates.format(jiajiaBanner.addtime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">-->
<!--                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
        </form>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>
    <th:block th:include="include :: summernote-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "jiajia/banner";
        $("#form-banner-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.modal.loading("图片上传中，请稍后...")
                $.operate.save(prefix + "/edit", $('#form-banner-edit').serialize());
            }
        }

        $("#file").bind('change', function () {
            $("#imgPhoto").show();
            $("#imgPhoto").attr("src",URL.createObjectURL($(this)[0].files[0]));

            uploadFile("#file",function (result) {
                $("#image").val(result.url);
            })
        });

        function uploadFile(idSelector,callback) {
            var formData = new FormData();
            formData.append('file',$(idSelector)[0].files[0]);
            $.ajax({
                url: "/common/admin/upload/up/single",
                data: formData,
                type: "post",
                dataType: "json",
                processData: false,
                contentType: false,
                success: function(result) {
                    callback(result)
                }
            })
        }

        $("input[name='addtime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function () {
            var value = document.getElementById('Del').value;
            var select =document.getElementById('isDel');
            if (value==1){
                select.options[0].selected="selected";
            }else {
                select.options[1].selected = "selected";
            }
        })

        $(function () {
            $('.summernote').each(function (i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function (contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function (files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function (result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function (error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
        });
    </script>
</body>
</html>